Skip to content

Conversation

@odow
Copy link
Member

@odow odow commented Nov 7, 2025

Closes #515

@codecov
Copy link

codecov bot commented Nov 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3a7aed2) to head (72df1c0).

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #516   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines         1154      1164   +10     
=========================================
+ Hits          1154      1164   +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@odow
Copy link
Member Author

odow commented Nov 7, 2025

Before

julia> @time sim!(nmpc, N, [180.0]; plant=plant, x_0=[0, 0], x̂_0=[0, 0, 0])
  0.250533 seconds (1.72 M allocations: 119.051 MiB, 3.97% gc time)
Simulation results of NonLinMPC with 35 time steps.

after

julia> @time sim!(nmpc, N, [180.0]; plant=plant, x_0=[0, 0], x̂_0=[0, 0, 0])
  0.192342 seconds (586.57 k allocations: 49.896 MiB, 2.61% gc time)
Simulation results of NonLinMPC with 35 time steps.

Code

using ModelPredictiveControl, JuMP
using Ipopt

N = 35 # number of solves/optimize! calls

function f!(ẋ, x, u, _ , p)
    g, L, K, m = p          # [m/s²], [m], [kg/s], [kg]
    θ, ω = x[1], x[2]       # [rad], [rad/s]
    τ  = u[1]               # [Nm]
    ẋ[1] = ω
    ẋ[2] = -g/L*sin(θ) - K/m*ω + τ/m/L^2
    return nothing
end
h!(y, x, _ , _ ) = (y[1] = 180/π*x[1]; nothing) # [°]
p = [9.8, 0.4, 1.2, 0.3]
nu, nx, ny, Ts = 1, 2, 1, 0.1
model = NonLinModel(f!, h!, Ts, nu, nx, ny; p)
p_plant = copy(p); p_plant[3] = p[3]*1.25
plant = NonLinModel(f!, h!, Ts, nu, nx, ny; p=p_plant)
Hp, Hc, Mwt, Nwt = 20, 2, [0.5], [2.5]
α=0.01; σQ=[0.1, 1.0]; σR=[5.0]; nint_u=[1]; σQint_u=[0.1]
σQint_ym = zeros(0)
umin, umax = [-1.5], [+1.5]
nc = Hp+1
transcription = MultipleShooting()
optim = Model(Ipopt.Optimizer); 
oracle = true
hessian = true
nmpc = NonLinMPC(model; 
    Hp, Hc, Mwt, Nwt, Cwt=Inf, transcription, oracle, hessian, optim,
    α, σQ, σR, nint_u, σQint_u, σQint_ym
)
nmpc = setconstraint!(nmpc; umin, umax)
unset_time_limit_sec(nmpc.optim)
sim!(nmpc, N, [180.0]; plant=plant, x_0=[0, 0], x̂_0=[0, 0, 0])
@time sim!(nmpc, N, [180.0]; plant=plant, x_0=[0, 0], x̂_0=[0, 0, 0])

@odow odow marked this pull request as ready for review November 7, 2025 01:04
@odow odow changed the title WIP: try to reuse existing data structures on re-solve Reuse existing data structures on re-solve Nov 7, 2025
@franckgaga
Copy link

Damn that was fast! Did you sleep, like, sometimes ? XP

Good job, seems to solve the "useless" initialize call at each iteration. It's cool that I will be able to keep my polymorphism behavior in linconstraint! and linconstrainteq! methods.

@odow
Copy link
Member Author

odow commented Nov 7, 2025

Did you sleep, like, sometimes

I live in New Zealand. Quiet Friday for me

@franckgaga
Copy link

Oh. lol.

@franckgaga
Copy link

My bad XD

@franckgaga
Copy link

franckgaga commented Nov 7, 2025

But past 4:20

if !has_nlp_constraints && !has_quadratic_constraints
Ipopt.AddIpoptStrOption(model.inner, "jac_c_constant", "yes")
Ipopt.AddIpoptStrOption(model.inner, "jac_d_constant", "yes")
if model.is_linear
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Jacobian can be constant, even if the objective is nonlinear ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Re-use initialised AD if bounds change

4 participants